1 Insurance Claims

data(AutoBi)

claims_tbl <- AutoBi
names(claims_tbl) <- claims_tbl %>% names %>% tolower

claims_tbl <- claims_tbl %>%
    mutate(lawyer   = (attorney == 1)
          ,male     = (clmsex   == 1)
          ,married  = (marital  == 1)
          ,seatbelt = (seatbelt == 1)
          ,age      = clmage
          ,insured  = (clminsur == 1)
          ,log_loss = log(loss)
           ) %>%
    filter(complete.cases(.))

model_stanlm <- stan_lm(log_loss ~ lawyer + seatbelt + male + insured + age
                       ,data   = claims_tbl
                       ,prior  = R2(location = 0.8)
                       )
## trying deprecated constructor; please alert package maintainer
## Warning: There were 2 divergent transitions after warmup. Increasing adapt_delta above 0.99 may help. See
## http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## Warning: Examine the pairs() plot to diagnose sampling problems

2 The 8-Schools Problem

eight_schools_tbl <- tribble(
     ~school, ~effect_mu, ~effect_se
    ,    "A",         25,         15  
    ,    "B",          8,         10  
    ,    "C",         -3,         16  
    ,    "D",          7,         11  
    ,    "E",         -1,          9  
    ,    "F",          1,         11  
    ,    "G",         18,         10  
    ,    "H",         12,         18  
)

ggplot(eight_schools_tbl) +
    geom_errorbar(aes(x = school, ymin = effect_mu - 2 * effect_se, ymax = effect_mu + 2 * effect_se), width = 0) +
    geom_point(aes(x = school, y = effect_mu)) +
    xlab("School") +
    ylab("Effect")

2.1 Centred Parameterisation

standata_lst <- list(
    J     = eight_schools_tbl %>% nrow
   ,y     = eight_schools_tbl$effect_mu
   ,sigma = eight_schools_tbl$effect_se
)
stan_cp_model <- stan_model('eight_schools_cp.stan')
## recompiling to avoid crashing R session
## In file included from /usr/local/lib/R/site-library/rstan/include/rstan/rstaninc.hpp:3:0,
##                  from file7d42798bc5ad.cpp:441:
## /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp: In function ‘int rstan::{anonymous}::command(rstan::stan_args&, Model&, Rcpp::List&, const std::vector<long unsigned int>&, const std::vector<std::__cxx11::basic_string<char> >&, RNG_t&)’:
## /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp:438:8: warning: ‘template<class> class std::auto_ptr’ is deprecated [-Wdeprecated-declarations]
##    std::auto_ptr<stan::io::var_context> init_context_ptr;
##         ^~~~~~~~
## In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
##                  from /usr/include/c++/7/locale:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical_streams.hpp:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical.hpp:54,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/try_lexical_convert.hpp:42,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast.hpp:32,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/tools/convert_from_string.hpp:15,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/constants/constants.hpp:13,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/scal/fun/constants.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/operator_unary_plus.hpp:7,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core.hpp:34,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/mat.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/src/stan/model/model_header.hpp:4,
##                  from file7d42798bc5ad.cpp:8:
## /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
##    template<typename> class auto_ptr;
##                             ^~~~~~~~
## In file included from /usr/local/lib/R/site-library/rstan/include/rstan/rstaninc.hpp:3:0,
##                  from file7d42798bc5ad.cpp:441:
## /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp:547:10: warning: ‘template<class> class std::auto_ptr’ is deprecated [-Wdeprecated-declarations]
##      std::auto_ptr<rstan_sample_writer> sample_writer_ptr;
##           ^~~~~~~~
## In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
##                  from /usr/include/c++/7/locale:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical_streams.hpp:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical.hpp:54,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/try_lexical_convert.hpp:42,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast.hpp:32,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/tools/convert_from_string.hpp:15,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/constants/constants.hpp:13,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/scal/fun/constants.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/operator_unary_plus.hpp:7,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core.hpp:34,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/mat.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/src/stan/model/model_header.hpp:4,
##                  from file7d42798bc5ad.cpp:8:
## /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
##    template<typename> class auto_ptr;
##                             ^~~~~~~~
stan_cp_stanfit <- sampling(
    stan_cp_model
   ,data   = standata_lst
   ,iter   = 500
   ,chains = 8
   ,seed   = stan_seed
)
## Warning: There were 33 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help. See
## http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## Warning: There were 4 chains where the estimated Bayesian Fraction of Missing Information was low. See
## http://mc-stan.org/misc/warnings.html#bfmi-low
## Warning: Examine the pairs() plot to diagnose sampling problems
traceplot(stan_cp_stanfit)

plot(stan_cp_stanfit)
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)

cp_np <- stan_cp_stanfit %>% nuts_params

mcmc_parcoord(stan_cp_stanfit %>% as.array, np = cp_np, alpha = 0.05)

2.2 Non-Centred Parameterisation

stan_ncp_model <- stan_model('eight_schools_ncp.stan')
## recompiling to avoid crashing R session
## In file included from /usr/local/lib/R/site-library/rstan/include/rstan/rstaninc.hpp:3:0,
##                  from file7d422033ad87.cpp:483:
## /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp: In function ‘int rstan::{anonymous}::command(rstan::stan_args&, Model&, Rcpp::List&, const std::vector<long unsigned int>&, const std::vector<std::__cxx11::basic_string<char> >&, RNG_t&)’:
## /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp:438:8: warning: ‘template<class> class std::auto_ptr’ is deprecated [-Wdeprecated-declarations]
##    std::auto_ptr<stan::io::var_context> init_context_ptr;
##         ^~~~~~~~
## In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
##                  from /usr/include/c++/7/locale:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical_streams.hpp:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical.hpp:54,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/try_lexical_convert.hpp:42,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast.hpp:32,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/tools/convert_from_string.hpp:15,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/constants/constants.hpp:13,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/scal/fun/constants.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/operator_unary_plus.hpp:7,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core.hpp:34,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/mat.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/src/stan/model/model_header.hpp:4,
##                  from file7d422033ad87.cpp:8:
## /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
##    template<typename> class auto_ptr;
##                             ^~~~~~~~
## In file included from /usr/local/lib/R/site-library/rstan/include/rstan/rstaninc.hpp:3:0,
##                  from file7d422033ad87.cpp:483:
## /usr/local/lib/R/site-library/rstan/include/rstan/stan_fit.hpp:547:10: warning: ‘template<class> class std::auto_ptr’ is deprecated [-Wdeprecated-declarations]
##      std::auto_ptr<rstan_sample_writer> sample_writer_ptr;
##           ^~~~~~~~
## In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
##                  from /usr/include/c++/7/locale:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical_streams.hpp:43,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/detail/converter_lexical.hpp:54,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast/try_lexical_convert.hpp:42,
##                  from /usr/local/lib/R/site-library/BH/include/boost/lexical_cast.hpp:32,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/tools/convert_from_string.hpp:15,
##                  from /usr/local/lib/R/site-library/BH/include/boost/math/constants/constants.hpp:13,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/scal/fun/constants.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/operator_unary_plus.hpp:7,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core.hpp:34,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/mat.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/stan/math.hpp:4,
##                  from /usr/local/lib/R/site-library/StanHeaders/include/src/stan/model/model_header.hpp:4,
##                  from file7d422033ad87.cpp:8:
## /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
##    template<typename> class auto_ptr;
##                             ^~~~~~~~
stan_ncp_stanfit <- sampling(
    stan_ncp_model
   ,data   = standata_lst
   ,iter   = 500
   ,chains = 8
   ,seed   = stan_seed
)

traceplot(stan_ncp_stanfit, pars = c('mu','tau','theta_tilde','theta'))

plot(stan_ncp_stanfit, pars = c('mu','tau','theta_tilde','theta'))
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)

ncp_np <- stan_ncp_stanfit %>% nuts_params

mcmc_parcoord(stan_ncp_stanfit %>% as.array, np = ncp_np, alpha = 0.05)

3 Thank You


mickcooney@gmail.com


https://github.com/kaybenleroll/dublin_r_workshops